package leetcode_core.leetcode_6;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

/**
 * 功能描述
 *
 * @author: 张庭杰
 * @date: 2023年02月19日 19:42
 */
public class LengthOfLongestSubstring {
    //滑动窗口法解题
    public int lengthOfLongestSubstring(String s) {
        int length = s.length();
        if(length == 0){
            return 0;
        }
        int[] window = new int[128];
        int l = 0,r = 0,max = 0;
        while(r < length){
            //窗口向右扩张
            int rightIndex = s.charAt(r++);
            window[rightIndex]++;
            while(window[rightIndex] > 1){
                int leftIndex = s.charAt(l++);
                window[leftIndex]--;
            }
            max = Math.max(max,r-l);
        }
        return max;
    }
}
